/* $RCSFile: HtmlEdit.aspx.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/AddonNice/PageModules/HTMLModule/HtmlEdit.aspx.cs $
 * $log$
 * Revision 20 2010/08/09 10:51:20  christian.surieux
 *   Added Log comment to all .aspx .ascx .master files
 * Revision 19 2010/08/09 09:43:54  christian.surieux
 *   Added Log to cs sources files
 * Revision 6 2010/08/06 19:56:25  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Globalization;

using AddonNice.Configuration;
using AddonNice.Diagnostics;
using AddonNice.Helpers;
using AddonNice.UI;
using AddonNice.UI.WebControls;
using AddonNice.UI.DataTypes;
using AddonNice.Settings.PageModules.HtmlModule;

using Trace2    =   System.Diagnostics.Trace;

namespace AddonNice.PageModules.HtmlModule
{

    public partial class HtmlEdit : EditItemPage
	{
        HtmlModuleDB HtmlModuleDB			= new HtmlModuleDB();
		protected AddonNice.UI.WebControls.IHtmlEditor DesktopText;

		/// <summary>
		/// Set the module guids with free access to this page
		/// </summary>
		protected override ArrayList AllowedModules
		{
			get
			{
				ArrayList al = new ArrayList();
				al.Add ("0B113F51-FEA3-499A-98E7-7B83C192FDBB");
				return al;
			}
		}

        /// <summary>
        /// The Page_Load event on this Page is used to obtain the ModuleID
        /// of the xml module to edit.
        /// It then uses the AddonNice.HtmlTextDB() data component
        /// to populate the page's edit controls with the text details.
        /// </summary>
        protected void Page_Load(object sender, System.EventArgs e)
        {
			if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]HtmlEdit Page_Load isPostBack: {0}.",IsPostBack),PageTraceSwitch.Sw.Info);
			DesktopText			    =   GetEditor(PlaceHolderHTMLEditor);

            if (!IsPostBack)
            {
                // Obtain a single row of text information
				SqlDataReader dr            =   null;
				try
				{
					dr                      =   HtmlModuleDB.GetHtmlTextGlobalized(ModuleID);

					if (dr.Read())
							DesktopText.TextContent     =  Server.HtmlDecode((String)dr["DesktopHtml"]);
					else	DesktopText.TextNoContent   =  LocalizeHelper.Localize(DocStr.HTMLDOCUMENT_TODO_ADDCONTENT, "Todo: Add Content...");
				}
				catch (Exception ex)
				{
					if (PageTraceSwitch.Sw.TraceError)
                        Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]HtmlEdit Page_Load  ex: {0}", ex),PageTraceSwitch.Sw.Info);
					throw;
				}
				finally
				{
					if (dr != null)
						dr.Close();
				}
              }
        }

        /// <summary>
        /// Save the text changes to the database.
        /// </summary>
        void HtmlEdit_Update(object sender, AddonNice.UI.ADNPageEventArgs e)
        {
			if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]HtmlEdit HtmlEdit_Update Content: '{0}'.",DesktopText.TextContent),PageTraceSwitch.Sw.Info);
			try
			{
				HtmlModuleDB.UpdateHtmlTextGlobalized(ModuleID, Server.HtmlEncode(DesktopText.TextContent));
			}
			catch (Exception ex)
			{
				if (PageTraceSwitch.Sw.TraceError)
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]HtmlEdit HtmlEdit_Update  ex : {0}", ex),PageTraceSwitch.Sw.Info);
				throw;
			}
        }

		#region Std overrides

        protected override void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }

        private void InitializeComponent() 
        {    
			Load	+=  new EventHandler(Page_Load);
            Update  +=  new PageEventHandler(HtmlEdit_Update);
		}

        protected override System.Web.UI.WebControls.PlaceHolder placeHolderForButtons
		{
			get {return PlaceHolderButtons; }
			set {}
		}

        protected override System.Web.UI.WebControls.PlaceHolder placeHolderForButtons2
		{
			get	{return PlaceHolderButtons2; }
			set {}
		}
		#endregion
    }
}

